<template>
  <el-dialog :title="'批量导入会员卡'" :close-on-click-modal="false" @closed="close()" :visible.sync="visible" width="450px">
    <el-form  ref="dataForm" label-width="100px">
      <el-form-item label="开卡店铺">
        <el-select v-model="data.shopId" clearable placeholder="请选择">
          <el-option
            v-for="item in shops"
            :key="item.id"
            :label="item.shopName"
            :value="item.id"
            :disabled="item.status !== 1">
          </el-option>
        </el-select>
      </el-form-item>
      <el-upload class="upload-demo" ref="upload"
        :action="url"
        :file-list="fileList"
        :data="data"
        :auto-upload="false"
        :before-upload="beforeUploadHandle"
        :on-success="successHandle"
        :on-change="changeHandle"
        :on-remove="removeHandle"
      >
        <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
        <div slot="tip" class="el-upload__tip">请选择Excel文件</div>
      </el-upload>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close()">取消</el-button>
      <el-button type="primary" :disabled="fileList.length != 1 || this.data.shopId == null" @click="submitUpload()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    props: {
      companyId: Number,
      shops: {
        type: Array,
        default: function(){
          return []
        }
      }
    },
    data() {
      return {
        visible: false,
        disabled: false,
        fileList: [],
        url: '',
        loading: null,
        data: {
          companyId: this.companyId,
          shopId: null,
        }
      }
    },
    activated() {
      this.url = this.$http.adornUrl(`/member/vipCard/import?token=${this.$cookie.get('token')}`)
    },
    methods: {
      init() {
        this.visible = true
        this.fileList = []
        this.url = this.$http.adornUrl(`/member/vipCard/import?token=${this.$cookie.get('token')}`)
        this.data.companyId = this.companyId
      },
      close() {
        this.visible = false;
        this.$emit('close');
      },
      submitUpload() {
        this.$refs.upload.submit();
      },

      // 上传之前
      beforeUploadHandle(file) {
        if(this.fileList.length != 1){
          this.$message.error('请选择文件')
          return false
        }
        if(this.data.companyId == null){
          this.$message.error('请选择公司')
          return false
        }
        if(this.data.shopId == null){
          this.$message.error('请选择开卡店铺')
          return false
        }
        this.loading = this.$loading({
          lock: true,
          text: '正在收银中，请稍后',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
      },
      removeHandle(file,fileList){
        this.fileList = []
      },
      changeHandle(file,fileList){
        if (!file.name || !file.name.endsWith('.xlsx')) {
          this.fileList = []
          this.$message.error('请选择xlsx的Excel文件')
          return false
        }
        if(fileList.length > 0){
          this.fileList.splice(0,1)
        }
        this.fileList.push(file)
      },
      // 上传成功
      successHandle(response, file, fileList) {
        if(this.loading != null){
          this.loading.close()
        }

        if (response && response.code === 0) {
          this.fileList = []
          this.$message.success('导入成功')
          this.visible = false
          this.$nextTick(() => {
            this.$emit('ok')
          })
        } else {
          var message = response.msg;
          this.$confirm(message, '提示', {
            confirmButtonText: '确定',
            dangerouslyUseHTMLString: true,
            type: 'error'
          }).then(() => {

          });
        }
      }
    }
  };
</script>
<style scoped lang="scss">
  .vip-card-rule {
    .input {
      width: 130px;
    }
  }
</style>
